<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style type="text/css">
        .class-title {
            margin-top: 10px;
            margin-left: 10px;
            margin-right: 10px;
            font-size: 20px;
            font-weight: bold;
            color: #00BFFF;
        }

        .class-hr {
            margin-top: 5px;
            height: 1px;
            border: none;
            border-top: 1px solid #DCDCDC;
        }

        .class-seqno {
            margin-top: 3px;
            margin-left: 10px;
            margin-right: 25px;
            font-size: 10px;
            color: #808080;
            font-weight: bold;
            text-align: right;
        }

        .class-table {
            width: 100%;
            text-align: left;
            color: black;
            font-size: 13px;
            margin-left: 2%;
            width: 100%;
            table-layout: fixed;
        }

        .class-td {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .lw {
            background-color: #DCE9F2;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
            font-weight: bold;
        }

        .lw1 {
            background-color: #f0f5f5;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
        }

        /* .table {
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
        }

        .table td {
            height: 40px;
            text-align: left;
            border: 0px solid #CBD6DE;
            padding: 0;
            margin: 0;
        } */

        .aui-searchbar-input {
            width: 95% !important;
        }
    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            安创入库
        </div>
    </header>
    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">容器条码</div>
                    <input type="text" class="aui-input" placeholder="请扫描容器条码" style="width: 80%;font-size: 16px;"
                        id="ctBarcode" onclick="getIdSelector();" />
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="padding-right:10px;font-size: 1.6rem"
                        onclick="doScan();"></i>
                </div>
            </li>

            <div class="aui-btn aui-btn-info aui-btn-block"><input type="text" class="aui-input"
                    style="width: 80%;font-size: 16px;" id="message" /></div>
            <div style="background-color:white;width: 100%;margin-top:2%;margin-bottom:5px;">
                <table class="table" style='width:100%;' id="table">
                    <thead>
                        <tr
                            style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 8px;font-weight: bold;">
                            <td algin='center'>物料编码</td>
                            <td algin='center'>规格</td>
                            <td algin='center'>数量</td>
                        </tr>
                    </thead>
                    <tbody id="tbodyHtml">
                    </tbody>
                </table>
            </div>
            <div class="aui-list-item-inner">
                <div class="aui-list-item-label" style="margin-left:30%;width:100%">已绑定容器数：</div>
                <div class="aui-list-item-input" style="margin-left:5%">
                    <input type="text" id="num" value="" disabled="true" style='color:red' />
                </div>
            </div>
        </ul>
        <ul class="ui-selectee" id="tbodyHtmlS" style="margin-bottom:5px;">
        </ul>
    </div>
    <div style="width:100%;margin-top:18%;"></div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()" id="submitButton">确认绑定</div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script>
    //光标所在id
    var idSelector = "";
    var num = 0;
    var arrSave = []
    let tableStr = []
    var arrSaveContainer = ''

    apiready = function () {
        // i am ready
        $("#ctBarcode").focus();
        idSelector = "ctBarcode";

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            if (idSelector == 'ctBarcode') {
                $("#ctBarcode").val(ret);
                checkBarCode(ret)
            }
        });
    }

    //PDA自带扫描回车事件
    $("#ctBarcode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#ctBarcode").blur();
                //调用条码解析+保存事件
                checkBarCode(tm);
            }
        }
    });

    function checkBarCode(tm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getContainerInfo";
        var data = {
            values: {
                container: tm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                for (var i = 0; i < arrSave.length; i++) {
                    if (tm == arrSave[i].boxnum) {
                        $("#ctBarcode").val("").focus().select();

                        api.toast({
                            msg: '容器已扫描,请检查！',
                            duration: 3000
                        });
                        return;
                    }
                }

                //成功
                if (myobj.code == '0') {
                    console.log("m", JSON.stringify(myobj))
                    var map = myobj.list;
                    $("#ctBarcode").val("").focus().select();
                    num = num + 1;
                    $("#num").val(num);
                    if (myobj.type == '1') {
                        for (var p in map) {
                            checkout(
                                map[p].mrlCode,
                                map[p].ext05,
                                map[p].qty,
                                map[p].ctBarcode,
                                map[p].drawing,
                                map[p].serialNum);
                        }
                    }
                } else if (myobj.code == '1') {

                    $("#ctBarcode").val("").focus().select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function checkout(mrlCode, lotCode, qty, boxnum, drawing, serialNum) {
        var model = {
            mrlCode: mrlCode, //物料号
            drawing: drawing, //规格
            lotCode: lotCode, //批次号
            qty: qty, //转移数量
            boxnum: boxnum, //容器
            serialNum: serialNum,
        }
        var model2 = {
            mrlCode: mrlCode, //物料号
            drawing: drawing, //规格
            lotCode: lotCode, //批次号
            qty: qty, //转移数量
            boxnum: boxnum, //容器
            serialNum: serialNum,
        }
        arrSave.push(model);
        tableStr.push(model2);
        console.log("arrSave", arrSave)
        $("#ctBarcode").val("");
        $("#ctBarcode").select();
        idSelector = "boxnum";
        setTable(arrSave);
    }

    function setTable(list) {
        var html = "";
        var arrSaveContainerNew = [];
        var arrSaveContainerStr = '';
        for (var i = 0; i < list.length; i++) {
            if (arrSaveContainerNew.indexOf(list[i].boxnum) == -1) {
                arrSaveContainerNew.push(list[i].boxnum);
                arrSaveContainerStr = arrSaveContainerStr + (arrSaveContainerStr == '' ? '' : ',') + list[i].boxnum;
            }

            html += "<li class='sortable ui-selectee ui-sortable'>";
            html += "<ul class='aui-list aui-media-list ui-selectee'>";
            html += "<li class='aui-list-item  ui-selectee'>";

            html += "<div class='aui-media-list-item-inner ui-selectee'>";
            html += "<div class='aui-list-item-inner ui-selectee'>";

            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>容器：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].boxnum + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料号：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrlCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>规格：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].drawing + "</div>";
            html += "</div>";
            // html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            // html += "<div class='aui-list-item-label ui-selectee list-tr-title'>批次号：</div>";
            // html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].lotCode + "</div>";
            // html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>数量：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].qty + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'></div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + '' + "</div>";
            html += "<div class='aui-list-item-right aui-btn ui-selectee' style='background:red;color:#f5f5f5;' onclick='remove(\"" + list[i].boxnum + "\");'>解绑</div>";
            html += "</div>";

            html += "</div>";
            html += "</li>";
            html += "</ul>";
            html += "</li>";
        }
        arrSaveContainer = arrSaveContainerStr;
        $api.html($api.byId("tbodyHtmlS"), html);
        setTable2(tableStr);
    };

    function setTable2(tableStr) {
        for (var i = 0; i < tableStr.length; i++) {
            var mrlCode = tableStr[i].mrlCode;
            for (var j = i + 1; j < tableStr.length; j++) {
                if (mrlCode == tableStr[j].mrlCode) {
                    tableStr[i].qty = Number(tableStr[i].qty) + Number(tableStr[j].qty);
                    tableStr.splice(j, 1);
                    j--;
                }
            }
        }
        var html = "";
        for (var i = 0; i < tableStr.length; i++) {
            html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
            html += "<td name='mrlCode'>" + tableStr[i].mrlCode + "</td>";
            html += "<td name='drawing'>" + tableStr[i].drawing + "</td>";
            html += "<td name='qty'>" + tableStr[i].qty + "</td>";
            html += "</tr>";
        }
        $api.html($api.byId("tbodyHtml"), html);
        if (tableStr.length == 0) {
            $("#num").val(0);
        }
    };

    function remove(boxnum) {
        num = num - 1;
        arrSave = arrSave.filter(item => item.boxnum !== boxnum)
        tableStr = tableStr.filter(item => item.boxnum !== boxnum)
        $("#num").val(num);
        setTable(arrSave);
        setTable2(tableStr)

    };

    //保存按钮
    function save() {
        if (arrSaveContainer.length == 0) {
            api.toast({
                msg: '没有需要保存的物料信息',
                duration: 3000
            });
            return;
        }

        var ctBarRedis = []
        var arrs = arrSaveContainer.split(",")
        arrs.forEach(item => {
            ctBarRedis.push(item);
        });
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/containerConsignmentSave";
        var data = {
            values: { //以表单方式提交参数（JSON对象）
                list: JSON.stringify(arrSave),
                containerList: arrSaveContainer,//容器、箱信息
                un: un
                , ctBarRedis: JSON.stringify(ctBarRedis)
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log("返回来啦:" + data);
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    api.toast({
                        msg: '绑定成功',
                        duration: 3000
                    });

                    arrSave = [];
                    tableStr = [];
                    num = 0
                    $("#num").val(0);

                    arrSaveContainer = '';

                    $("#ctBarcode").val("");
                    arrSave = []
                    tableStr = []
                    //回显保存结果
                    setTable(arrSave);
                    setTable2(tableStr)
                    $("#message").html(myobj.message);
                    $("#ctBarcode").focus();
                    idSelector = "boxnum";
                } else if (myobj.code == '1') {
                    $("#ctBarcode").val("");
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
            }
        });
    }
</script>

</html>